package com.ruoyi.environment.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.environment.mapper.OpmEnvRuleMapper;
import com.ruoyi.environment.service.IOpmEnvRuleService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 环保法规Service业务层处理
 *
 * @author cjie
 * @date 2021-07-23
 */
@Service
public class OpmEnvRuleServiceImpl extends LinkFileServiceImpl implements IOpmEnvRuleService {
    @Autowired
    private OpmEnvRuleMapper opmEnvRuleMapper;

    /**
     * 查询环保法规
     *
     * @param id 环保法规ID
     * @return 环保法规
     */
    @Override
    public OpmEnvRule selectOpmEnvRuleById(Long id) {
        OpmEnvRule result = opmEnvRuleMapper.selectOpmEnvRuleById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询环保法规列表
     *
     * @param opmEnvRule 环保法规
     * @return 环保法规
     */
    @Override
    public List<OpmEnvRule> selectOpmEnvRuleList(OpmEnvRule opmEnvRule) {
        return opmEnvRuleMapper.selectOpmEnvRuleList(opmEnvRule);
    }

    /**
     * 新增环保法规
     *
     * @param opmEnvRule 环保法规
     * @return 结果
     */
    @Override
    public int insertOpmEnvRule(OpmEnvRule opmEnvRule) {
        opmEnvRule.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmEnvRule.setCreateTime(DateUtils.getNowDate());
        opmEnvRule.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmEnvRule.getFileIdList(),Long.toString(opmEnvRule.getId()));
        return opmEnvRuleMapper.insertOpmEnvRule(opmEnvRule);
    }

    /**
     * 修改环保法规
     *
     * @param opmEnvRule 环保法规
     * @return 结果
     */
    @Override
    public int updateOpmEnvRule(OpmEnvRule opmEnvRule) {
        opmEnvRule.setUpdateTime(DateUtils.getNowDate());
        opmEnvRule.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmEnvRule.getFileIdList(),Long.toString(opmEnvRule.getId()));
        return opmEnvRuleMapper.updateOpmEnvRule(opmEnvRule);
    }

    /**
     * 批量删除环保法规
     *
     * @param ids 需要删除的环保法规ID
     * @return 结果
     */
    @Override
    public int deleteOpmEnvRuleByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmEnvRuleMapper.deleteOpmEnvRuleByIds(ids);
    }

    /**
     * 删除环保法规信息
     *
     * @param id 环保法规ID
     * @return 结果
     */
    @Override
    public int deleteOpmEnvRuleById(Long id) {
        return opmEnvRuleMapper.deleteOpmEnvRuleById(id);
    }
}
